package com.cs.pk.codestriker;

import org.eclipse.swt.widgets.Combo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.cs.pk.Activator;
import com.cs.pk.utils.Constants;
import com.cs.pk.utils.UtilRepository;

public class Obsoletes extends CodeStriker {
	
	private String obsoleteAction = null;
	private String obsoleteTagName = null;
	private String obsoleteAttribute = null;
	private Combo obsoletes = null; 
	public Obsoletes(Object obs) {
		super(obs);
		obsoletes = (Combo)obs;
		obsoleteAction = Activator.getDefault().getDefaultPreference(Constants.OBSOLETEACTION);
		obsoleteTagName = Activator.getDefault().getDefaultPreference(Constants.OBSOLETETAGNAME);
		obsoleteAttribute = Activator.getDefault().getDefaultPreference(Constants.OBSOLETEATTRIBUTE);
	}
	
	protected void getPreferredList(Object projectno) {
		try {
			Document obsoleteDoc = getResourceListFromServer(getBaseUrl()+obsoleteAction.replaceAll("<PROJECT_NO>", projectno.toString()));
			NodeList obsoleteList = obsoleteDoc.getElementsByTagName(obsoleteTagName);
			for (int i = 0; i < obsoleteList.getLength(); i++) {
				Element project = (Element)obsoleteList.item(i);
				UtilRepository.updateObsoleteRepository(project.getTextContent(), project.getAttribute(obsoleteAttribute));
				obsoletes.add(project.getTextContent());
			}
			obsoletes.add("<<N/A>>", 0);
			obsoletes.select(0);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	
	
	public void clear() {
		obsoletes.removeAll();
	}

}
